package com.dd;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.chantel.tool.db.DBUtil;
import com.dd.exception.XiangMuNotFoundException;

public class XiangMu {
	// 项目ID
	private int id;
	
	private int xiangMuShiYeID;

	// 名称
	private String mingCheng;
	
	//缩略图
	private String tuPian;

	// [TuPian] [image] NULL ,
	private String beiZhu;

	// 状态
	private String zhuangTai;

	// 项目开始时间
	private Date kaiShiShiJian;

	private Date jieShuShiJian;

	private String jianCheng;

	private int shenPiZhongShuMu;

	private int shenPiTongGuoShuMu;

	private int shenPiJuJueShuMu;
	
	private int denDaiShengPiShuMu;
	
	//sql 
	private String GET_ALL_XIANGMU_SQL = "select * from XiangMuTable";

	public int getDenDaiShengPiShuMu() {
		return denDaiShengPiShuMu;
	}

	public void setDenDaiShengPiShuMu(int denDaiShengPiShuMu) {
		this.denDaiShengPiShuMu = denDaiShengPiShuMu;
	}

	public XiangMu(int id) throws XiangMuNotFoundException{
		this.id = id;
		this.loadFromDb();
	}
	
	public XiangMu()
	{
		
	}
	public XiangMu(String mingCheng, String beiZhu, String zhuangTai, Date kaiShiShiJian, Date jieShuShiJian, String jianCheng, int shenPiZhongShuMu, int shenPiTongGuoShuMu, int shenPiJuJueShuMu) {
		super();
		this.mingCheng = mingCheng;
		this.beiZhu = beiZhu;
		this.zhuangTai = zhuangTai;
		this.kaiShiShiJian = kaiShiShiJian;
		this.jieShuShiJian = jieShuShiJian;
		this.jianCheng = jianCheng;
		this.shenPiZhongShuMu = shenPiZhongShuMu;
		this.shenPiTongGuoShuMu = shenPiTongGuoShuMu;
		this.shenPiJuJueShuMu = shenPiJuJueShuMu;
	}
	
	public XiangMu(Map m)
	{
		makeXiangMuFromMap(m);
	}
	private void makeXiangMuFromMap(Map m)
	{
		int id = ((Integer) m.get("XiangMuId")).intValue();
		int xiangMuBuId = ((Integer) m.get("XiangMuShiYeBuID")).intValue();
		String beiZhu = (String)m.get("BeiZhu");
		String mingCheng = (String)m.get("MingCheng");
		String jianCheng = (String)m.get("JianCheng");
		String tuPian = (String)m.get("TuPian");
		
		String zhuangTai = (String)m.get("ZhuangTai");
		
		Date beginTime = (Date)m.get("KaiShiShiJian");
		int nDengDaiShuMu = ((Integer)m.get("DengDaiShenPiShuMu")).intValue();
		int nBiaoDanZhongshu = ((Integer)m.get("ShenPiZhongShuMu")).intValue();
		int nBiaoDanTongGuoShu = ((Integer)m.get("ShenPiJuJueShuMu")).intValue();
		int nBiaoDanJuJueShu = ((Integer)m.get("ShenPiJuJueShuMu")).intValue();
		
		
		this.setId(id);
		this.setXiangMuShiYeID(xiangMuBuId);
		this.setBeiZhu(beiZhu);
		this.setTuPian(tuPian);
		this.setJianCheng(jianCheng);
		this.setMingCheng(mingCheng);
		this.setZhuangTai(zhuangTai);
		this.setKaiShiShiJian(beginTime);
		this.setShenPiJuJueShuMu(nBiaoDanJuJueShu);
		this.setShenPiZhongShuMu(nBiaoDanZhongshu);
		this.setShenPiTongGuoShuMu(nBiaoDanTongGuoShu);
		this.setDenDaiShengPiShuMu(nDengDaiShuMu);
	}

	public String getBeiZhu() {
		return beiZhu;
	}

	public void setBeiZhu(String beiZhu) {
		this.beiZhu = beiZhu;
	}

	public String getTuPian() {
		return tuPian;
	}

	public void setTuPian(String beiZhu) {
		this.tuPian = beiZhu;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getXiangMuShiYeID() {
		return xiangMuShiYeID;
	}

	public void setXiangMuShiYeID(int id) {
		this.xiangMuShiYeID = id;
	}

	public String getJianCheng() {
		return jianCheng;
	}

	public void setJianCheng(String jianCheng) {
		this.jianCheng = jianCheng;
	}

	public Date getJieShuShiJian() {
		return jieShuShiJian;
	}

	public void setJieShuShiJian(Date jieShuShiJian) {
		this.jieShuShiJian = jieShuShiJian;
	}

	public Date getKaiShiShiJian() {
		return kaiShiShiJian;
	}

	public void setKaiShiShiJian(Date kaiShiShiJian) {
		this.kaiShiShiJian = kaiShiShiJian;
	}

	public String getMingCheng() {
		return mingCheng;
	}

	public void setMingCheng(String mingCheng) {
		this.mingCheng = mingCheng;
	}

	public int getShenPiJuJueShuMu() {
		return shenPiJuJueShuMu;
	}

	public void setShenPiJuJueShuMu(int shenPiJuJueShuMu) {
		this.shenPiJuJueShuMu = shenPiJuJueShuMu;
	}

	public int getShenPiTongGuoShuMu() {
		return shenPiTongGuoShuMu;
	}

	public void setShenPiTongGuoShuMu(int shenPiTongGuoShuMu) {
		this.shenPiTongGuoShuMu = shenPiTongGuoShuMu;
	}

	public int getShenPiZhongShuMu() {
		return shenPiZhongShuMu;
	}

	public void setShenPiZhongShuMu(int shenPiZhongShuMu) {
		this.shenPiZhongShuMu = shenPiZhongShuMu;
	}

	public String getZhuangTai() {
		return zhuangTai;
	}

	public void setZhuangTai(String zhuangTai) {
		this.zhuangTai = zhuangTai;
	}
	
	public static XiangMu GetXiangMusById(int id)
	{
		DBUtil du = DBUtil.getInstance();
		
		String sql = "select* from XiangMuTable where XiangMuId = " + id;
		Map m = du.getRow(sql);
		
		String beiZhu = (String)m.get("BeiZhu");
		String mingCheng = (String)m.get("MingCheng");
		String jianCheng = (String)m.get("JianCheng");
			
		XiangMu xm = new XiangMu();
		xm.setId(id);
		xm.setBeiZhu(beiZhu);
		xm.setJianCheng(jianCheng);
		xm.setMingCheng(mingCheng);
		
		return xm;
	}
	
	public ArrayList GetAllXiangMus()
	{
		DBUtil du = DBUtil.getInstance();

		String sql = GET_ALL_XIANGMU_SQL;
		List l = du.getRecords(sql);
		
		return (ArrayList)l;
	}
	
	public  ArrayList GetAllXiangMusOld()
	{
		DBUtil du = DBUtil.getInstance();  
		ArrayList aL = new ArrayList(); 
		String sql = "select* from XiangMuTable"; 
		List l = du.getRecords(sql); 
		Iterator it = l.iterator();
		while(it.hasNext())
		{ 
			Map m = (Map)it.next(); 
			XiangMu xm = new XiangMu(m); aL.add(xm); 
		} 
		return 	aL; 
	}  

	public boolean InsertXiangMu(String mingCheng,int ShiYeBuID,String jianChen,String beiZhu,String tupian) {
		
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();
		// 获取唯一ID，改数据库ID不为自动生成
		this.id = SequenceManager.nextID(SysGlobals.XM_ID);
		this.kaiShiShiJian = new java.util.Date();
		m.put("XiangMuId", new Integer(this.id));
		m.put("XiangMuShiYeBuID", new Integer(ShiYeBuID));
		Date newDate = new Date();
		m.put("KaiShiShiJian", newDate);
		m.put("MingCheng", mingCheng);
		m.put("JianCheng", jianChen);
		//String strTuPian = "Not used";
		m.put("TuPian", tupian);
		String strZhuangTai = "Open";
		m.put("ZhuangTai", strZhuangTai);
		m.put("BeiZhu", beiZhu);
		int intShumu = 0;
		m.put("ShenPiZhongShuMu", new Integer(intShumu) );
		m.put("ShenPiTongGuoShuMu", new Integer(intShumu));
		m.put("ShenPiJuJueShuMu", new Integer(intShumu));
		m.put("DengDaiShenPiShuMu", new Integer(intShumu));
	    
		// do action
		boolean bool_res = du.insertRecord("XiangMuTable", m);
		return bool_res;
	}

	public boolean UpdateShenPiZhongShuMu(int xiangMuId) {
		DBUtil du = DBUtil.getInstance();
		
		String getSql = "select * from XiangMuTable where XiangMuId = " + xiangMuId;
		Map map = du.getRow(getSql);
		
		if (map == null || map.size() <= 0)
			return false;
		
		int shuMu = 0;
		Object  o = map.get("ShenPiZhongShuMu");
		if(null != o)
		{
			shuMu = ((Integer)o).intValue();	
		}
			
		++shuMu;
		
		String updateWhere = "Id = " + shuMu;
		
		Map m = new HashMap();
		m.put("ShenPiZhongShuMu", new Integer(shuMu));
		
		boolean bool_res = du.updateFieldsValue("XiangMuTable", m,
				updateWhere);
		
		return bool_res;
	}
	
	public static boolean UpdateTongGuoShuMu(int xiangMuId) {
		DBUtil du = DBUtil.getInstance();
		
		String getSql = "select * from XiangMuTable where XiangMuId = " + xiangMuId;
		Map map = du.getRow(getSql);
		
		if (map == null || map.size() <= 0)
			return false;
		
		int shuMu = 0;
		Object  o = map.get("ShenPiTongGuoShuMu");
		if(null != o)
		{
			shuMu = ((Integer)o).intValue();	
		}
			
		++shuMu;
		
		String updateWhere = "XiangMuId = " + xiangMuId;
		
		Map m = new HashMap();
		m.put("ShenPiTongGuoShuMu", new Integer(shuMu));
		
		boolean bool_res = du.updateFieldsValue("XiangMuTable", m,
				updateWhere);
		
		return bool_res;
	}
	
	public static boolean UpdateJuJueShuMu(int xiangMuId) {
		DBUtil du = DBUtil.getInstance();
		
		String getSql = "select * from XiangMuTable where XiangMuId = " + xiangMuId;
		Map map = du.getRow(getSql);
		
		if (map == null || map.size() <= 0)
			return false;
		
		int shuMu = 0;
		Object  o = map.get("ShenPiJuJueShuMu");
		if(null != o)
		{
			shuMu = ((Integer)o).intValue();	
		}
			
		++shuMu;
		
		String updateWhere = "XiangMuId = " + xiangMuId;
		
		Map m = new HashMap();
		m.put("ShenPiJuJueShuMu", new Integer(shuMu));
		
		boolean bool_res = du.updateFieldsValue("XiangMuTable", m,
				updateWhere);
		
		return bool_res;
	}

	public boolean DeleteXiangMu() {
		// TODO delete
		return true;
	}

	public void loadFromDb() throws XiangMuNotFoundException {
		
		DBUtil du = DBUtil.getInstance();
		
		String getSql = "select * from XiangMuTable where XiangMuId = " + this.id;
		Map map = du.getRow(getSql);
		if (map == null || map.size() <= 0)
			throw new XiangMuNotFoundException();
		
		this.makeXiangMuFromMap(map);
	}
	//
	//该用户是否有权限查看该项目的详细信息
	//
	public boolean isAuthorizationForThisProject(int userId)
	{
	DBUtil du = DBUtil.getInstance();
		
		String getSql = "select * from YuanGongXiangMuTable where XiangMuId = " + this.id + " and YuanGongId = " + userId;
		Map map = du.getRow(getSql);
		if (map == null || map.size() <= 0)
			return false;
		
		return true;
	}
	
	public static void main(String[] args)
	{
		XiangMu newXiangmu = new XiangMu();
		newXiangmu.InsertXiangMu("Project3", 2,"JP3", "BeiZhu CeShi3 ddddd", "no image");
	}	
}
